package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.GoodsCategory;
import cn.signalpha.modules.shop.service.GoodsCategoryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
 * 商品
 *
 * @author suguangjun
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/goods/category")
@Api(value = "商品分类", tags = "商品分类")
public class ApiGoodsCategoryController extends BaseController {

    private final GoodsCategoryService goodsCategoryService;

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name="parentId", dataType = "string", value = "上级id 0为顶级分类"),
	})
	@ApiOperation(value = "列表", notes = "根据pid返回")
	public R<List<GoodsCategory>> categoryList(@ApiIgnore @RequestParam Map<String, Object> params) {
    	List<GoodsCategory> list = goodsCategoryService.list(Wrappers.<GoodsCategory>query().lambda()
			.eq(ObjectUtil.isNotEmpty(params.get("parentId")), GoodsCategory::getParentId, params.get("parentId"))
			.eq(GoodsCategory::getStatus, 1).orderByDesc(GoodsCategory::getSort)
		);
		return R.data(list);
	}

	@GetMapping("/tree")
	@ApiOperation(value = "树", notes = "")
	public R categoryTree(@ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("status", "1");
		return R.data(goodsCategoryService.tree(params));
	}

}
